//logs.js
const util = require('../../utils/util.js')
const app = getApp()
const net = app.globalData.net

Page({
  data: {
    logs: [],
    telphone:'',
    password:'',
    type:1,
  },
  tologNum: function () {
    wx.redirectTo({
      url: '../logNum/logNum',
    })
  },
  onLoad: function () {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return util.formatTime(new Date(log))
      })
    })
  },
  login:function(ev){
    console.log(this.data.telphone)
    console.log(this.data.password)
    if(this.data.telphone && this.data.password){
      wx.request({
        // 发起请求，提交登录信息
        url: net + '/user/login', //王林写好的接口地址
        data: {
          telphone: this.data.telphone,
          password: this.data.password,
          type: this.data.type
        },
        success(res) { // 请求成功执行的函数
          console.log(res.data)
          if (res.data.state) {
            console.log("登录成功")
            wx.reLaunch({
              url: '../home/home'
            })
          }else{
            console.log("登录失败")
            wx.showToast({
              title: res.data.info,
              icon: 'none'
            })
          }
        },
        fail(){
          wx.showToast({
            title: '网络繁忙，请稍后重试',
            icon: 'none'
          })
        },
        method: 'POST'
      })
    } else if (!this.data.telphone){
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
    } else{
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
    }
    
  },
  telphone:function(ev){
    this.setData({
      telphone: ev.detail.value,
    })
  },
  password: function (ev) {
    this.setData({
      password: ev.detail.value,
    })
  }
})
